package com.ebook.server.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户管理的Service层
 * @author   潘吉友
 * @Date	 2013-2-25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TestService {

//	@Resource
//	private JdbcTemplate jdbcTemplate;
//	
	private static Log logger = LogFactory.getLog(TestService.class);

//	public boolean addTest(){
//		String sql = "insert into test1 (age) values (?)";
//		SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate);   
//		logger.info(sql);
//				try{
//					insert.setTableName("test1");
//					insert.setGeneratedKeyName("id");
//					Map<String,Object> map = new HashMap<String,Object>();
//					map.put("age", 15);
//					Number count =  insert.executeAndReturnKey(map);
//					System.out.println(count);
//				}catch(Exception e){
//					logger.error("添加数据时发生错误!"+e.getMessage());
//					throw new RuntimeException(e);
//				}
//				return true;
//	}
	
	public boolean add(){
		String[] configLocations = new String[] {
                "classpath:applicationContext.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
        JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
		String sql = "insert into test1 (age) values (?)";
		logger.info(sql);
		jdbcTemplate.update(sql, 13);
		return true;
	}
	
	
}
